/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.ibrae.excelparser1.mainlogics.Logging.implimentation;

import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;

import ru.ibrae.excelparser1.gui.Main;
import ru.ibrae.excelparser1.mainlogics.Logging.ILogger;

import java.io.File;
import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.logging.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Priority;

public class ComplexLogger
        implements ILogger {
    //~ Static fields/initializers ---------------------------------------------

    static Logger l = Logger.getRootLogger();
    //~ Instance fields --------------------------------------------------------
    Boolean b;
    String m;
    File log;
    boolean printFile = false;
    int iter = 0;
    RollingFileAppender rollingFileAppender = null;

    //~ Methods ----------------------------------------------------------------
    public void setLogFile(File source) {
        int index = source.getAbsolutePath().lastIndexOf(".");

        if (( index > 0 ) && ( index <= ( source.getAbsolutePath().length() - 2 ) )) {
            final String logFileName = source.getAbsolutePath().substring(0, index)
                    + ".log";
            this.log = new File(logFileName);
        }

        if (iter != 0) {
            printFile = true;
        }
    }

    @Override
    public Boolean log(String message) {
        m = message;

        if (rollingFileAppender == null) {
            try {
                rollingFileAppender = new RollingFileAppender(new PatternLayout(),
                        log.getAbsolutePath());
                rollingFileAppender.setMaxFileSize("1MB");
                rollingFileAppender.setAppend(true);
                rollingFileAppender.setFile(log.getAbsolutePath());
                l.removeAllAppenders();
                l.addAppender(rollingFileAppender);
            }
            catch (IOException ex) {
                java.util.logging.Logger.getLogger(ComplexLogger.class.getName()).log(Level.SEVERE, null, ex);
            }

            iter++;
        }

        if (printFile) {
            //rollingFileAppender = new RollingFileAppender(new PatternLayout(),                                                              log.getAbsolutePath());
            rollingFileAppender.setFile(log.getAbsolutePath());

            /*l.removeAllAppenders();
            l.addAppender(rollingFileAppender);*/
            Main.appendToLog(
                    "Лог импорта записывается в файл \n" + log.getAbsolutePath());
            printFile = !printFile;
        } else if (( iter == 1 ) && !printFile) {
            Main.appendToLog(
                    "Лог импорта записывается в файл \n" + log.getAbsolutePath());
            iter++;
        }

        l.info(
                new SimpleDateFormat("[MM-dd HH:mm:ss] \n").format(new Date())
                + message);

        return Main.appendToLog(message);
    }
}